//
//  VideoUploadManager.m
//  Teacher
//
//  Created by 马腾飞 on 15/8/20.
//  Copyright (c) 2015年 xueyinyue. All rights reserved.
//

#import "VideoUploadManager.h"
#import "ApplicationContext.h"
#import "User.h"

#import <QiniuSDK.h>

static NSString *const kVideoUploadManagerToken = @"VideoToken";
static NSString *const kVideoUploadManagerTitle = @"VideoTitle";
static NSString *const kVideoUploadManagerKey = @"VideoKey";
static NSString *const kVideoUploadManagerURL = @"VideoURL";
static NSString *const kPreviousUserName = @"UserName";
static NSString *const kQiNiuTempFilePath = @"qiniutemp";

@implementation VideoUploadManager

static VideoUploadManager *sharedObj = nil;

+ (void)createUploadManager
{
    NSError *error = nil;
    QNFileRecorder *fileRecorder = [QNFileRecorder fileRecorderWithFolder:[NSTemporaryDirectory() stringByAppendingString:kQiNiuTempFilePath] error:&error];
    NSLog(@"recorder error %@", error);
    
    sharedObj = [[VideoUploadManager alloc] initWithRecorder:fileRecorder recorderKeyGenerator:^NSString *(NSString *uploadKey, NSString *filePath) {
        return [NSString stringWithFormat:@"%@",uploadKey];
    }];
}

+ (instancetype)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [VideoUploadManager createUploadManager];
    });
    return sharedObj;
}

- (void)saveVideoData
{
    User *user = [ApplicationContext instance].currentUser;
  //  [[NSUserDefaults standardUserDefaults] setValue:user.user forKey:kPreviousUserName];
    [[NSUserDefaults standardUserDefaults] setValue:_uploadVideoURL.path forKey:kVideoUploadManagerURL];
    [[NSUserDefaults standardUserDefaults] setValue:_token forKey:kVideoUploadManagerToken];
    [[NSUserDefaults standardUserDefaults] setValue:_key forKey:kVideoUploadManagerKey];
    [[NSUserDefaults standardUserDefaults] setValue:_videoTitle forKey:kVideoUploadManagerTitle];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)getVideoData
{
    NSString *urlStr = [[NSUserDefaults standardUserDefaults] valueForKey:kVideoUploadManagerURL];
    _uploadVideoURL = [NSURL URLWithString:urlStr];
    _token = [[NSUserDefaults standardUserDefaults] valueForKey:kVideoUploadManagerToken];
    _key = [[NSUserDefaults standardUserDefaults] valueForKey:kVideoUploadManagerKey];
    _videoTitle = [[NSUserDefaults standardUserDefaults] valueForKey:kVideoUploadManagerTitle];
    NSLog(@"_uploadVideoURL:%@\n_token:%@\n_key:%@\n_videoTitle:%@",_uploadVideoURL,_token,_key,_videoTitle);
}

- (void)clearVideoData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *qiniuTempPath = [NSTemporaryDirectory() stringByAppendingString:kQiNiuTempFilePath];
        NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:qiniuTempPath];
        for (NSString *p in files)
        {
            NSError *error;
            NSString *path = [qiniuTempPath stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager] fileExistsAtPath:path])
            {
                [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
            }
        }
    });
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPreviousUserName];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kVideoUploadManagerKey];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kVideoUploadManagerTitle];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kVideoUploadManagerToken];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kVideoUploadManagerURL];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)needContinueUploading
{
    User *user = [ApplicationContext instance].currentUser;
    NSString *previousUser = [[NSUserDefaults standardUserDefaults] valueForKey:kPreviousUserName];
//    if ([[NSUserDefaults standardUserDefaults] valueForKey:kVideoUploadManagerToken] && [previousUser isEqualToString:user.user])
//    {
//        return YES;
//    }
    return NO;
}

@end
